package com.prabhakar.taxiclubapp;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.string;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class FareSummary extends Activity {

	// Progress Dialog
	//private ProgressDialog progressDialog;
	JSONParser jsonParser = new JSONParser();
	private static final String TAG = "FareSummary";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if(("0").equalsIgnoreCase(FareBean.getEconomy_Day_baseFare())){
			new FetchTCFaresAsync().execute();
			Log.v(TAG,">> FetchTCFaresAsync().execute() Async call ..<<");
		
		}else{
			Log.v(TAG,">> postFareBeanUpdate mthd call ..<<");
			postFareBeanUpdate();
		}
		
	}

	
	protected void postFareBeanUpdate() {
		setContentView(R.layout.fare_summary);
	
		TextView textView1 = (TextView)findViewById(R.id.economy_day_fare);
		textView1.setText(FareBean.getEconomy_Day_baseFare());
		
		TextView textView2 = (TextView)findViewById(R.id.economy_day_rateperkm);
		textView2.setText(FareBean.getEconomy_Day_ratePerKm());
		
		TextView textView3 = (TextView)findViewById(R.id.economy_night_fare);
		textView3.setText(FareBean.getEconomy_Night_baseFare());
		
		TextView textView4 = (TextView)findViewById(R.id.economy_night_rateperkm);
		textView4.setText(FareBean.getEconomy_Night_ratePerKm());
		
		TextView textView5 = (TextView)findViewById(R.id.sedan_day_fare);
		textView5.setText(FareBean.getSedan_Day_baseFare());
		
		TextView textView6 = (TextView)findViewById(R.id.sedan_day_rateperkm);
		textView6.setText(FareBean.getSedan_Day_ratePerKm());
		
		TextView textView7 = (TextView)findViewById(R.id.sedan_night_fare);
		textView7.setText(FareBean.getSedan_Night_baseFare());
		
		TextView textView8 = (TextView)findViewById(R.id.sedan_night_rateperkm);
		textView8.setText(FareBean.getSedan_Night_ratePerKm());
		
		Button okButton = (Button)findViewById(R.id.okBtn);
		okButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.fare_summary, menu);
		return true;
	}


	/*
	 * private Class that runs  the AsynTask
	 */
	private class FetchTCFaresAsync extends AsyncTask<Void, Void, String>{

		private static final String Tag ="FetchTCFaresAsync";
		String response;
		JSONArray  jsonResponse = null;
		private ProgressDialog progressDialog;
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			progressDialog = new ProgressDialog(FareSummary.this);
			progressDialog.setMessage("Contacting Remote Server for Updated Fares ...");
			progressDialog.setCancelable(true);
			progressDialog.show();
		}

		@Override
		protected String doInBackground(Void... params) {

			jsonResponse = new JSONArray();
			List<NameValuePair> param = new ArrayList<NameValuePair>();
			param.add(new BasicNameValuePair(Constants.PRICING_Param_NAME ,Constants.PricingTabValue_P2P));	

			try {   
				jsonResponse = jsonParser.makeHttpFareRequest(Constants.TC_FareURL, "POST",param );
			}catch (Exception e) {
				e.printStackTrace();
			}
			Log.d(" Fare Response", jsonResponse.toString());

			return jsonResponse.toString();
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if(null !=result) response = "Fares Received Successfully from Remote Server";
			progressDialog.setMessage(response);
			progressDialog.dismiss();
			Log.v(Tag,"RESPONSE /result  recevd in onPostExecute() >> "+result);
			parseJsonArray_And_SetFareDetails(result);
		}


		private void parseJsonArray_And_SetFareDetails(String response2) {

			String cartype="";
			String timeOfDay = "";
			JSONObject json = null;
			JSONObject innerJson =null;
			String innerJsonString ="";
			try {
				JSONArray  jsonResponse = new JSONArray(response2);
				Log.v(Tag,"RESPONSE /result  recevd in parseJsonArray() >> "+jsonResponse.toString());
				Log.v(Tag,"jsonResponse.length()>> "+jsonResponse.length());

				for(int i=0;i<jsonResponse.length();i++){
					json =	new JSONObject(jsonResponse.optString(i).toString());
					innerJsonString = json.getString(Constants.Data);
					innerJson = new JSONObject(innerJsonString);
					Log.v(Tag,""+i+"th innerJson >> "+innerJson);

					if(innerJson!=null){
						cartype =innerJson.getString(Constants.CARTYPE);
						timeOfDay = innerJson.getString(Constants.Time);

						if(cartype.equalsIgnoreCase(Constants.SEDAN) && timeOfDay.equalsIgnoreCase(Constants.DAY)){
							FareBean.setSedan_Day_ratePerKm(innerJson.getString(Constants.RatePerKm));
							FareBean.setSedan_Day_baseFare(innerJson.getString(Constants.BaseFare));
						}
						if(cartype.equalsIgnoreCase(Constants.SEDAN) && timeOfDay.equalsIgnoreCase(Constants.NIGHT)){
							FareBean.setSedan_Night_baseFare(innerJson.getString(Constants.BaseFare));
							FareBean.setSedan_Night_ratePerKm(innerJson.getString(Constants.RatePerKm));
						}
						if(cartype.equalsIgnoreCase(Constants.ECONOMY) && timeOfDay.equalsIgnoreCase(Constants.DAY)){
							FareBean.setEconomy_Day_baseFare(innerJson.getString(Constants.BaseFare));
							FareBean.setEconomy_Day_ratePerKm(innerJson.getString(Constants.RatePerKm));
						}
						if(cartype.equalsIgnoreCase(Constants.ECONOMY) && timeOfDay.equalsIgnoreCase(Constants.NIGHT)){
							FareBean.setEconomy_Night_baseFare(innerJson.getString(Constants.BaseFare));
							FareBean.setEconomy_Night_ratePerKm(innerJson.getString(Constants.RatePerKm));
						}
					}

				}

				Log.v(Tag,"Sedan_Day fare + rate >> "+FareBean.getSedan_Day_baseFare() + " - "+FareBean.getSedan_Day_ratePerKm());
				Log.v(Tag,"Sedan_Night fare + rate >> "+FareBean.getSedan_Night_baseFare()+" - "+FareBean.getSedan_Night_ratePerKm());
				Log.v(Tag,"Economy_Day fare + rate >> "+FareBean.getEconomy_Day_baseFare() + " - "+FareBean.getEconomy_Day_ratePerKm());
				Log.v(Tag,"Economy_Night fare + rate >> "+FareBean.getEconomy_Night_baseFare()+" - "+FareBean.getEconomy_Night_ratePerKm());

				// set the xml data..
				postFareBeanUpdate();
				
			} catch (JSONException e) {
				e.printStackTrace();
			}

		}




	}
}
